package org.notepad.activity;

import org.notepad.R;
import org.notepad.adapter.TimeTrackerAdapter;
import org.notepad.bean.TimeRecord;
import org.notepad.persistence.TimeRecordOpenHelper;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ListView;

public class TimeTracker extends Activity {
	TimeTrackerAdapter timeTrackerAdapter;
	public static final int TRACKER_ENTRY_REQUEST_CODE = 1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.time_list_item);
		
		ListView listView = (ListView) findViewById(R.id.records_list);
		timeTrackerAdapter = new TimeTrackerAdapter();
		listView.setAdapter(timeTrackerAdapter);
		TimeRecordOpenHelper openHelper = new TimeRecordOpenHelper(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.activity_time_tracker, menu);
		return true;
	}
	
	public boolean onMenuItemSelected(int featureId,MenuItem m){
		if(m.getItemId() == R.id.add_tracker_menu_item){
			Intent intent = new Intent(this,AddTimeTrackerActivity.class);
			startActivityForResult(intent,TRACKER_ENTRY_REQUEST_CODE);
			return true;
		}
		return super.onMenuItemSelected(featureId, m);
	}
	
	@Override
	protected void onActivityResult(int requestCode,int resultCode,Intent data){
		if(requestCode == TRACKER_ENTRY_REQUEST_CODE){
			if(resultCode == RESULT_OK){
				String time = data.getStringExtra("time");
				String note = data.getStringExtra("note");
				timeTrackerAdapter.addTimeTracker(new TimeRecord(time,note));
				timeTrackerAdapter.notifyDataSetChanged();
			}
		}
	}
	
}
